package com.blb.examsystem.dao.Impl;

import com.blb.common.entity.Question;
import com.blb.common.utils.DBUtils;
import com.blb.examsystem.dao.QuestionDao;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class QuestionDaoImpl implements QuestionDao {

    @Override
    public int selectCount() {
        return DBUtils.executeCount("select count(*) from t_question");
    }

    @Override
    public List<Question> selectLimit(int start, int length) {
        return DBUtils.executeQuery(Question.class,"select * from t_question limit ?,?",
                start,length);
    }

    @Override
    public int selectCountByContent(String content) {
        return DBUtils.executeCount("select count(*) from t_question where content like ?",
                "%"+content+"%");
    }

    @Override
    public List<Question> selectLimitByContent(String content, int start, int length) {
        return DBUtils.executeQuery(Question.class,"select * from t_question where content like ? limit ?,?",
                "%"+content+"%",start,length);
    }

    @Override
    public int insertQuestion(Question question) {
        return DBUtils.executeUpdate("insert into t_question(content,option_a,option_b,option_c,option_d,answer,difficulty,question_set_id,image) values(?,?,?,?,?,?,?,?,?)",
                question.getContent(),question.getOptionA(),question.getOptionB(),question.getOptionC(),
                question.getOptionD(),question.getAnswer(),question.getDifficulty(),question.getQuestionSetId(),
                question.getImage());
    }

    @Override
    public List<Question> selectById(int id) {
        return DBUtils.executeQuery(Question.class,"select * from t_question where id=?",id);
    }

    @Override
    public int updateQuestion(Question question) {
        return DBUtils.executeUpdate("update t_question set content=?,option_a=?,option_b=?,option_c=?,option_d=?,answer=?,difficulty=?,question_set_id=?,image=? where id=?",
                question.getContent(),question.getOptionA(),question.getOptionB(),question.getOptionC(),
                question.getOptionD(),question.getAnswer(),question.getDifficulty(),question.getQuestionSetId(),
                question.getImage(),question.getId());
    }

    @Override
    public int deleteQuestion(int id) {
        return DBUtils.executeUpdate("delete from t_question where id=?",id);
    }
}
